package com.atguigu.lease.web.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.atguigu.lease.model.entity.AttrKey;
import com.atguigu.lease.model.entity.AttrValue;
import com.atguigu.lease.web.admin.mapper.AttrKeyMapper;
import com.atguigu.lease.web.admin.service.AttrKeyService;
import com.atguigu.lease.web.admin.vo.attr.AttrKeyVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author liubo
* @description 针对表【attr_key(房间基本属性表)】的数据库操作Service实现
* @createDate 2023-07-24 15:48:00
*/
@Service
public class AttrKeyServiceImpl extends ServiceImpl<AttrKeyMapper, AttrKey>
    implements AttrKeyService{
    @Override
    public List<AttrKeyVo> selectall() {
        List<AttrKey> list1=list();
        List<AttrValue> list2 = Db.lambdaQuery(AttrValue.class).list();
        Map<Long, List<AttrValue>> listMap=new HashMap<>();
        //把vo分组变成id的map集合
        if(CollUtil.isNotEmpty(list2))
        {
            listMap = list2.stream().collect(Collectors.groupingBy(AttrValue::getAttrKeyId));
        }
        List<AttrKeyVo> vo=new ArrayList<>(list1.size());
        for (AttrKey attrKey : list1) {
            AttrKeyVo v=new AttrKeyVo();
            v.setId(attrKey.getId());
            v.setName(attrKey.getName());
            v.setAttrValueList(listMap.get(attrKey.getId()));
            vo.add(v);
        }
        return vo;
    }
}




